﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;
using System.Data;

namespace Visa.Service
{
    public class NewsService
    {
        private VisaEntities db = new VisaEntities();

        public int addOrUpdateNews(News entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void DeleteNews(int id)
        {
            var c = db.News.SingleOrDefault(l => l.Id == id);
            c.Deleted = true;
            db.SaveChanges();
        }

        public News GetNewsById(int id)
        {
            try
            {
                return db.News.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public List<News> GetAllNews()
        {
            try
            {
                return db.News.Where(o => o.Deleted == false).ToList();
            }
            catch { return null; }
        }
    }
}
